package com.fengye.algorithms.leecode;

/**
 * @Author fengye
 * @Date 2023/4/10 9:26
 **/
public class BinarySearch704 {
    public int search(int[] nums, int target) {
        int n = nums.length;
        if(nums[n-1] < target) {
            return -1;
        }
        if(nums[0] > target) {
            return -1;
        }

        int start = 0;
        int end = n-1;
        while(start<=end) {
            int mid = (end+start)/2;
            if(nums[mid] == target) {
                return mid;
            } else if(nums[mid] > target) {
                end = mid-1;
            } else {
                start = mid + 1;
            }
        }
        return -1;
    }

}
